
#include "tetrisbox.h"

tetrisbox::tetrisbox(QWidget*parent):QWidget(parent)
{
//对block初始化
    for(int i=0;i<COUNT;i++){
    block.x[i]=-1;
    block.y[i]=-1;
}
    block.centerX=-1;
    block.centerY=-1;
    block.ID=-1;
    //对box进行初始化
    for (int i = 0; i < MAXX; ++i) {
    for (int j = 0; j < MAXY; ++j) {
        box[i][j]=0;
    }
    }
    //设置本游戏窗口的高度和宽度
    //设置背景色·黑
    int w=tetris::getWidth();
    int h=tetris::getHeight();
    setFixedSize(w,h);
    setPalette(QPalette(Qt::darkYellow));
    setAutoFillBackground(true);
}
void tetrisbox::updateTetris(tetris tetris){
    //更新block
    block=tetris.getBlock();
    //更新box
    for (int i = 0; i < MAXX; ++i) {
    for(int j=0;j<MAXY;j++){
        box[i][j]=tetris.getBox(i,j);
    }
    }
    repaint();
}

void tetrisbox::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPen pen;
    QBrush brush;
    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(255,255,255));
    brush.setStyle(Qt::SolidPattern);
    brush.setColor(QColor(255,255,255));
    painter.setPen(pen);
    painter.setBrush(brush);
    //绘制box中的内容
    for(int i=0;i<MAXX;i++){
    for (int j = 0; j < MAXY; ++j) {
        if( box[i][j]==1){
            int x=i*WIDTH+i*INTERVAL;
            int y=j*HEIGHT+j*INTERVAL;
            painter.drawRect(x,y,WIDTH,HEIGHT);
        }
    }
    }
    //绘制block中的内容
    for(int i=0;i<COUNT;i++){
    int x=block.x[i];
    int y=block.y[i];
    int x1=x*WIDTH+x*INTERVAL;
    int y1=y*HEIGHT+y*INTERVAL;
    painter.drawRect(x1,y1,WIDTH,HEIGHT);
    }
}
